package com.drondea.testclient.service.provider;

import cn.hutool.core.util.StrUtil;
import com.drondea.sms.channel.ChannelSession;
import com.drondea.sms.common.SequenceNumber;
import com.drondea.sms.common.util.CommonUtil;
import com.drondea.sms.message.IMessage;
import com.drondea.sms.message.smpp34.SmppSubmitSmRequestMessage;
import com.drondea.testclient.panel.SmppPanel;
import com.drondea.testclient.util.counter.SmppAtomicUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @author shd
 * @date 2022/7/14 10:00
 */

@Slf4j
public class SmppClientMessageProvider extends AbstractClientMessageProvider {

    private static final SmppPanel PANEL = SmppPanel.getInstance();

    @Override
    public List<IMessage> getTcpMessages(ChannelSession channelSession) {
        SmppSubmitSmRequestMessage requestMessage = (SmppSubmitSmRequestMessage) MESSAGES_QUEUE.poll();
        if (requestMessage == null) {
            return null;
        }
        SequenceNumber sequenceNumber = channelSession.getSequenceNumber();
        requestMessage.getHeader().setSequenceNumber(sequenceNumber.next());

        //切分长短信
        List<IMessage> longMsgSlices = CommonUtil.getLongMsgSlices(requestMessage, channelSession.getConfiguration(), sequenceNumber);
        PANEL.sendCountLabel.setText(StrUtil.format("发送数量: {}", SmppAtomicUtil.SEND_COUNT.addAndGet(longMsgSlices.size())));
        return longMsgSlices;
    }
    
}
